<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['activiti:deployment:import']"
            >部署</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" ></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="deploymentList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="流程编号" align="center" key="id" prop="id"  />
          <el-table-column label="部署编号" align="center" key="deploymentId" prop="deploymentId"  />
          <el-table-column label="KEY" align="center" key="key" prop="key" />
          <el-table-column label="名称" align="center" key="name" prop="name"  />
          <el-table-column label="流程文件" align="center" key="sourceName" prop="sourceName" width="120" />
          <el-table-column label="流程图片" align="center" key="image" prop="image">
            <template scope="scope">
              <div class="demo-image__preview">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="'data:image/gif;base64,'+scope.row.image"
                  :preview-src-list="['data:image/gif;base64,'+scope.row.image]">
                </el-image>
              </div>
              <!--<img :src="'data:image/gif;base64,'+scope.row.image" style="width: auto"  class="login-code-img"/>-->
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.userId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['activiti:deployment:remove']"
              >删除</el-button>
              <el-button
                v-if="scope.row.userId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleApply(scope.row)"
                v-hasPermi="['activiti:deployment:apply']"
              >申请</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".zip, .rar"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getToken } from "@/utils/auth";
  import { list,apply,remove } from "@/api/activiti/deployment";
  export default {
    name: "User",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        deploymentList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/activiti/deployment/deploy"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        imageUrl:""
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        list(this.queryParams).then(response => {
          this.deploymentList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "用户导入";
        this.upload.open = true;
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.msgSuccess(response.msg);
       /* this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });*/
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit()
      },
      handleApply(row){
        apply(row.id).then(response => {
          this.msgSuccess(response.msg);
        })
      },
      handleDelete(row){
        remove(row.deploymentId).then(response => {
          this.msgSuccess(response.msg);
          this.getList();
        })
      }
    }
  };
</script>
